package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.yw.YwLoanAbandonMapper;
import com.zjdiepu.www.ibiz.yw.IYwLoanAbandonService;
import com.zjdiepu.www.model.yw.dto.YwLoanAbandon;
import com.zjdiepu.www.model.yw.ext.YwLoanAbandonExt;

/**
 * 弃贷Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月09日 09时29分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwLoanAbandonServiceImpl extends BaseService implements IYwLoanAbandonService {
	public static final Logger logger = LoggerFactory.getLogger(YwLoanAbandonServiceImpl.class);

	@Autowired
	private YwLoanAbandonMapper ywLoanAbandonMapper;
	
	@Override
	public void saveUpdateAndDelete(YwLoanAbandon ywLoanAbandon, OperateType operType) {
		switch (operType) {
		case add:
			ywLoanAbandon.initInsertData(ywLoanAbandon.getModifier(), ywLoanAbandon.getModifierName());
			ywLoanAbandonMapper.insert(ywLoanAbandon);
			break;
		case edit:
			ywLoanAbandon.initUpdateData(ywLoanAbandon.getModifier(), ywLoanAbandon.getModifierName());
			// 影响的记录数
			int updateCount = ywLoanAbandonMapper.updateByPrimaryKeySelective(ywLoanAbandon);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywLoanAbandon.initDeleteData(ywLoanAbandon.getModifier(), ywLoanAbandon.getModifierName());
			// 影响的记录数
			int deleteCount = ywLoanAbandonMapper.updateByPrimaryKeySelective(ywLoanAbandon);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwLoanAbandon> selectByExt(YwLoanAbandonExt ywLoanAbandonExt) {
		return ywLoanAbandonMapper.selectByExt(ywLoanAbandonExt);
	}
	
	@Override
	public int countByExt(YwLoanAbandonExt ywLoanAbandonExt){
		return ywLoanAbandonMapper.countByExt(ywLoanAbandonExt);
	}
	
	@Override
	public YwLoanAbandon selectById(String id) {
		return ywLoanAbandonMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywLoanAbandonMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwLoanAbandon ywLoanAbandon) {
		return ywLoanAbandonMapper.countRefined(ywLoanAbandon);
	}

	@Override
	public YwLoanAbandon selectRefined(YwLoanAbandon ywLoanAbandon) {
		return ywLoanAbandonMapper.selectRefined(ywLoanAbandon);
	}
	
	@Override
	public int deletedRefined(YwLoanAbandon ywLoanAbandon) {
		return ywLoanAbandonMapper.deletedRefined(ywLoanAbandon);
	}

	@Override
	public boolean repeat(YwLoanAbandon ywLoanAbandon) {
		int count = countRefined(ywLoanAbandon);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywLoanAbandon.getId())) {
			return true;
		}
		YwLoanAbandon dbItem = selectRefined(ywLoanAbandon);
		if(null == dbItem) {
			return false;
		}
		return !ywLoanAbandon.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywLoanAbandonMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwLoanAbandon> ywLoanAbandons) {
		return ywLoanAbandonMapper.insertBatch(ywLoanAbandons);
	}
}
